package com.huawei.hianalytics.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.huawei.hianalytics.g.b;

public class e {
    public static SharedPreferences a(Context context, String str) {
        return context.getSharedPreferences(b(context, str), 0);
    }

    public static void a(SharedPreferences sharedPreferences, String str, Object obj) {
        if (sharedPreferences == null || str == null || str.isEmpty()) {
            b.b("HiAnalyticsSharedPreference", "sp/spKey is null");
            return;
        }
        Editor edit = sharedPreferences.edit();
        if (obj instanceof Boolean) {
            edit.putBoolean(str, ((Boolean) obj).booleanValue());
        } else if (obj instanceof Float) {
            edit.putFloat(str, ((Float) obj).floatValue());
        } else if (obj instanceof Integer) {
            edit.putInt(str, ((Integer) obj).intValue());
        } else if (obj instanceof Long) {
            edit.putLong(str, ((Long) obj).longValue());
        } else {
            edit.putString(str, (String) obj);
        }
        edit.commit();
    }

    public static Object b(SharedPreferences sharedPreferences, String str, Object obj) {
        if (sharedPreferences == null || str == null || str.isEmpty()) {
            return "";
        }
        try {
            return obj instanceof Boolean ? Boolean.valueOf(sharedPreferences.getBoolean(str, ((Boolean) obj).booleanValue())) : obj instanceof Float ? Float.valueOf(sharedPreferences.getFloat(str, ((Float) obj).floatValue())) : obj instanceof Integer ? Integer.valueOf(sharedPreferences.getInt(str, ((Integer) obj).intValue())) : obj instanceof Long ? Long.valueOf(sharedPreferences.getLong(str, ((Long) obj).longValue())) : obj instanceof String ? sharedPreferences.getString(str, (String) obj) : obj;
        } catch (ClassCastException e) {
            b.c("HiAnalyticsSharedPreference", "getInfoFromSP() class cast Exception !");
            return obj;
        }
    }

    public static String b(Context context, String str) {
        return "hianalytics_" + str + "_" + context.getPackageName();
    }
}
